package com.matriq.cloudmanager;

import org.apache.http.client.CookieStore;

import com.matriq.cloudmanager.R;
import com.matriq.cloudmanager.entity.User;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import android.app.Application;
import net.lgpage.tool.ImageTool;
import net.lgpage.tool.SharedPreferencesTool;
import net.lgpage.util.CircleBitmapDisplayer;
import net.lgpage.util.CrashHandler;
import net.lgpage.util.Saver;
import net.lgpage.util.VPNUtil;

/*
 * 全局类,保存app全局变量
 */
public class MyApp extends Application {

	public final static int CAPTURE = 101;

	String username = "201603107002";

	boolean isVpnLogin;
	boolean notwifiplay;
	boolean push;

	public boolean isVpnLogin() {
		return isVpnLogin;
	}

	public void setVpnLogin(boolean isVpnLogin) {
		this.isVpnLogin = isVpnLogin;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	User loginObj;

	public User getLoginObj() {
		return loginObj;
	}

	public void setLoginObj(User loginObj) {
		this.loginObj = loginObj;
	}

	private static MyApp app;
	CookieStore cookieStore;
	CrashHandler crashHandler;
	@Override
	public void onCreate() {
		super.onCreate();
		crashHandler = CrashHandler.getInstance();
		//crashHandler.init(getApplicationContext());
		app = this;
		ImageTool.initImageLoader(this);
		setOptions();
		Saver.loadCookies(this);
		SharedPreferencesTool tool  = new SharedPreferencesTool(getApplicationContext());
		push = (boolean) tool.load("push", true);
		notwifiplay = (boolean) tool.load("notwifiplay", true);
		VPNUtil vpn = new VPNUtil(MyApp.this);
		vpn.setVPN();
	}
	
	public boolean isNotwifiplay() {
		return notwifiplay;
	}

	public void setNotwifiplay(boolean notwifiplay) {
		this.notwifiplay = notwifiplay;
	}

	public boolean isPush() {
		return push;
	}

	public void setPush(boolean push) {
		this.push = push;
	}

	public CrashHandler getCrashHandler() {
		return crashHandler;
	}

	@Override
	public String toString() {
		return "MyApp{" + "loginObj=" + loginObj + ", cookieStore="
				+ cookieStore + ", ip='" + ip + '\'' + '}';
	}

	DisplayImageOptions modelOption;
	DisplayImageOptions circleOption;
	DisplayImageOptions adOption;
	DisplayImageOptions recommendOption;
	private void setOptions() {
		modelOption = new DisplayImageOptions.Builder().cacheOnDisk(true)
				.cacheInMemory(false).showImageOnLoading(R.drawable.default_model)
				.showImageForEmptyUri(R.drawable.default_model)
				.showImageOnFail(R.drawable.default_model)
				.resetViewBeforeLoading(true)
				.build();
		circleOption = new DisplayImageOptions.Builder().cacheInMemory(true)
				.cacheOnDisk(true).displayer(new CircleBitmapDisplayer())
				.build();
	}

	public MyApp(User loginObj) {

		this.loginObj = loginObj;
	}

	public MyApp() {

	}

	public static String sharedName = "appinfo";

	public CookieStore getCookieStore() {
		return cookieStore;
	}

	public void setCookieStore(CookieStore cookieStore) {
		this.cookieStore = cookieStore;
	}

	public static MyApp getApp() {
		if (app == null) {
			app = new MyApp();
		}
		return app;
	}

	private String ip = "121.40.112.88:8080";

	// private String ip = "192.168.118.114:8080";
	public String getRootUrl() {
		return "http://" + ip + "/DuoBao/";
	}

	public DisplayImageOptions getModelOption() {
		return modelOption;
	}

	public void setModelOption(DisplayImageOptions modelOption) {
		this.modelOption = modelOption;
	}

	public DisplayImageOptions getCircleOption() {
		return circleOption;
	}

	public void setCircleOption(DisplayImageOptions circleOption) {
		this.circleOption = circleOption;
	}

	public DisplayImageOptions getAdOption() {
		return adOption;
	}

	public void setAdOption(DisplayImageOptions adOption) {
		this.adOption = adOption;
	}

	public String getUrl() {
		return getRootUrl();
	}

	String locCity;

	public String getLocCity() {
		return locCity;
	}

	public void setLocCity(String locCity) {
		this.locCity = locCity;
	}

	public void outLogin() {
		loginObj = null;
		//Saver.clearCookies(this);
	}

}
